package com.simple.datax.transform.tcs;

import java.util.HashMap;
import java.util.Map;

import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;

import net.easipay.ec.v2.ECFlow201Document;
import net.easipay.ec.v2.ECFlow201ResponseDocument;

import com.simple.datax.api.TransformerException;
import com.simple.datax.transform.util.TransformUtils;
import com.simple.datax.xml.ec.EC101MessageDocument;
import com.simple.datax.xml.ec.EC101MessageDocument.EC101Message;
import com.simple.datax.xml.tcs.TCS101MessageDocument;
import com.simple.datax.xml.tcs.TCS101MessageDocument.TCS101Message;
import com.simple.datax.xml.tcs.XMLDATA;

/**
 * @Title: Ec2TcsMessageTransformer.java
 * @Package com.simple.datax.transform.tcs
 * @Description: 从Ec到TCS的转换报文头, 只有出的时候使用！！！！
 * @author Paladin Chen
 * @date 2013-1-8 下午3:28:21
 * @version V1.0
 */
public class Ec2TcsMessageTransformer {
	public TCS101MessageDocument transform(EC101MessageDocument xmlDoc) throws TransformerException {
		EC101Message ecmsg = ((EC101MessageDocument) xmlDoc).getEC101Message();
		com.simple.datax.xml.ec.MessageHeadDocument.MessageHead ecMessageHead = ecmsg.getMessageHead();

		TCS101MessageDocument tcsmessageDoc = TCS101MessageDocument.Factory.newInstance();
		TCS101Message tcsmessage = tcsmessageDoc.addNewTCS101Message();
		com.simple.datax.xml.tcs.MessageHeadDocument.MessageHead tcsMessageHead = tcsmessage.addNewMessageHead();

		tcsMessageHead.setMessageId(TransformUtils.changeNullToBlank(ecMessageHead.getMessageId()));
		tcsMessageHead.setMessageType(TransformUtils.changeNullToBlank(ecMessageHead.getMessageType()));
		tcsMessageHead.setMessageTime(TransformUtils.changeNullToBlank(ecMessageHead.getMessageTime()));
		tcsMessageHead.setSenderId(TransformUtils.changeNullToBlank(ecMessageHead.getSenderId()));
		tcsMessageHead.setSenderAddress(TransformUtils.changeNullToBlank(ecMessageHead.getSenderAddress()));
		tcsMessageHead.setReceiverId(TransformUtils.changeNullToBlank(ecMessageHead.getReceiverId()));
		tcsMessageHead.setReceiverAddress(TransformUtils.changeNullToBlank(ecMessageHead.getReceiverAddress()));

		tcsMessageHead.setDataCompress(TransformUtils.changeNullToBlank(ecMessageHead.getDataCompress()));

		tcsMessageHead.setDataEncoding(TransformUtils.changeNullToBlank(ecMessageHead.getDataEncoding()));

		tcsMessageHead.setDataResponse(ecMessageHead.getDataResponse());
		tcsMessageHead.setDataUnwrap(ecMessageHead.getDataUnwrap());

		try {
			// 2013-2-28 修改支持去掉ecdata中的namespace
			XmlOptions xo = new XmlOptions();
			xo.setUseDefaultNamespace();
			Map<String, String> nms = new HashMap<String, String>();
			nms.put("ec", "http://www.easipay.net/ec/v2");
			nms.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
			xo.setSaveImplicitNamespaces(nms);
			xo.setSavePrettyPrint();

			String tmp = ecmsg.getMessageBody().xmlText(xo);

			XmlObject ecBody = null;
			try {
				ecBody = XmlObject.Factory.parse(tmp);
			} catch (Exception e) {
				// 支持不含ec和xsi的xml
				ecBody = XmlObject.Factory.parse(ecmsg.getMessageBody().xmlText());
			}

			if (ecBody instanceof ECFlow201Document) {
				Ec2TcsFlowTransformer e2ttransformer = new Ec2TcsFlowTransformer();

				tcsmessage.setMessageBody(XMLDATA.Factory.parse(e2ttransformer.transform((ECFlow201Document) ecBody)
						.toString()));
			} else if (ecBody instanceof ECFlow201ResponseDocument) {
				throw new TransformerException("FLow201Response EC 2 TCS Not Need NOW, Calling wrong function.");
			}
		} catch (Exception e) {
			throw new TransformerException("Error occur when transform ec xml body." + e.getMessage(), e);
		}

		return tcsmessageDoc;
	}
}
